/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.university.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.university.entity.DisciplineFields;
import com.thinkgem.jeesite.modules.university.entity.XueLiLevel;
import com.thinkgem.jeesite.modules.university.service.DisciplineFieldsService;

/**
 * 学科门类Controller
 * @author 朱俊吉
 * @version 2017-12-08
 */
@Controller
@RequestMapping(value = "${adminPath}/university/disciplineFields")
public class DisciplineFieldsController extends BaseController {

	@Autowired
	private DisciplineFieldsService disciplineFieldsService;
	
	@ModelAttribute
	public DisciplineFields get(@RequestParam(required=false) String id) {
		DisciplineFields entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = disciplineFieldsService.get(id);
		}
		if (entity == null){
			entity = new DisciplineFields();
		}
		return entity;
	}
	
	@RequiresPermissions("university:disciplineFields:view")
	@RequestMapping(value = {"list", ""})
	public String list(DisciplineFields disciplineFields, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DisciplineFields> page = disciplineFieldsService.findPage(new Page<DisciplineFields>(request, response), disciplineFields); 
		model.addAttribute("page", page);
		return "modules/university/disciplineFieldsList";
	}

	@RequiresPermissions("university:disciplineFields:view")
	@RequestMapping(value = "form")
	public String form(DisciplineFields disciplineFields, Model model) {
		model.addAttribute("disciplineFields", disciplineFields);
		return "modules/university/disciplineFieldsForm";
	}

	@RequiresPermissions("university:disciplineFields:edit")
	@RequestMapping(value = "save")
	public String save(DisciplineFields disciplineFields, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, disciplineFields)){
			return form(disciplineFields, model);
		}
		disciplineFieldsService.save(disciplineFields);
		addMessage(redirectAttributes, "保存学科门类成功");
		return "redirect:"+Global.getAdminPath()+"/university/disciplineFields/?repage";
	}
	
	@RequiresPermissions("university:disciplineFields:edit")
	@RequestMapping(value = "delete")
	public String delete(DisciplineFields disciplineFields, RedirectAttributes redirectAttributes) {
		disciplineFieldsService.delete(disciplineFields);
		addMessage(redirectAttributes, "删除学科门类成功");
		return "redirect:"+Global.getAdminPath()+"/university/disciplineFields/?repage";
	}
	
	@ResponseBody
	@RequestMapping(value = "listDataBySelectedXueLiLevel")
	public List<DisciplineFields> listDataBySelectedXueLiLevel(@RequestParam(required=false) String selected) {
		DisciplineFields disciplineFields = new DisciplineFields();
		disciplineFields.setXueLiLevel(new XueLiLevel(selected));
		return disciplineFieldsService.findList(disciplineFields);
	}

}